<template>
    <div class="enterpriseForm">
        <el-form :model="form" :rules="rules" ref="form" label-width="90px" class="demo-ruleForm">
            <el-row>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="机构名称" prop="companyDto.name">
                        <el-input v-model="form.companyDto.name" placeholder="请输入机构名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="othIpt">
                    <el-form-item label="统一社会信用代码" prop="companyDto.cpCode">
                        <el-input maxlength="18" v-model="form.companyDto.cpCode" placeholder="请输入统一社会信用代码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="用户名" prop="user.nickName">
                        <el-input maxlength="18" v-model="form.user.nickName" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="设置密码" prop="user.password">
                        <el-popover placement="right" trigger="focus" width="240">
                            <p v-show="passwordIndex == -1">
                                请至少输入6个字符。请不要使用容易被猜到的密码。
                            </p>

                            <div v-show="passwordIndex == 0">
                                <p style="font-size: 16px; color: #e47470">强度：太短</p>
                                <el-progress :percentage="50" status="exception"></el-progress>
                                <p style="padding-top: 5px">
                                    请至少输入6个字符。请不要使用容易被猜到的密码。
                                </p>
                            </div>

                            <div v-show="passwordIndex == 1">
                                <p style="font-size: 16px; color: #e47470">强度：弱</p>
                                <el-progress :percentage="50" status="exception"></el-progress>
                                <p style="padding-top: 5px">
                                    请至少输入6个字符。请不要使用容易被猜到的密码。
                                </p>
                            </div>

                            <div v-show="passwordIndex >= 2">
                                <p style="font-size: 16px; color: #7ebf50">强度：安全</p>
                                <el-progress :percentage="100" status="success"></el-progress>
                                <p style="padding-top: 5px">
                                    请至少输入6个字符。请不要使用容易被猜到的密码。
                                </p>
                            </div>

                            <el-input slot="reference" v-model="form.user.password" placeholder="请输入登录密码"
                                maxlength="16" show-password>
                            </el-input>
                        </el-popover>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="登录账号" prop="user.userName">
                        <el-input v-model="form.user.userName" @blur="handleOnlyVal('用户账号', form.user.userName)"
                            placeholder="请输入登录账号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="确认密码" prop="user.checkpassword">
                        <el-input show-password v-model="form.user.checkpassword" placeholder="确认密码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item label="手机号" prop="user.phone">
                        <el-input v-model="form.user.phone" @blur="handleOnlyVal('手机号码', form.user.phone)"
                            placeholder="请输入手机号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <el-form-item ref="headimgUpload1" label="头像" prop="user.avatar">
                        
                        <el-upload class="avatar-uploader" :action="imageUploadUrl" :show-file-list="false"
                            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :on-error="handleError">
                            <img v-if="imgUrl" :src="imgUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                        <p class="uploadTip">上传文件不超过10M，且只能上传jpg、png类型</p>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div class="registerBtn">
            <el-button @click="prevBtn()">上一步</el-button>
            <el-button class="submitBtn colorBtn" :loading="loading" @click="handleSubmit()" type="primary">立即注册</el-button>
        </div>
    </div>
</template>
<script>
import { enterpriseRegNew, getOnly, sendPhoneCode } from "@/request/api/register"
import { aesEncrypt } from "@/utils/crypto.js";
import { sendEmail2 } from "@/request/api/login"
import { keyVal } from "@/request/api/link"
import rulesUtil from "@/utils/rules"
export default {
    data() {
        // 确认密码
        var checkPass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.form.user.password) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            loading:false,
            passwordIndex: -1,
            typeData: [],
            imageUploadUrl: '/dev-api/file/upload/uploadNotQR',
            imgUrl:'',
            form: {
                type: "1", //注册类型(0个人用户 1机构用户 2专家) 
                user: {
                    //个人/机构/专家 公用信息字段 
                    nickName: "", //用户名  必填
                    userName: "", //用户账号 必填
                    password: "", //用户密码-必填 加密模式为AES加密（AES/ECB/PKCS5Padding）key为：!Ae357S07&iKk#24"
                    phone: "", //手机号 非必填
                    avatar: "", //头像地址 必填
                    iuiCertification: "0", //是否实名认证 0未实名 1实名，实名的话以下字段都必
                },
                companyDto: {
                    //机构注册信息字段
                    name: "", //机构名称
                    cpCode: "" //统一社会信用代码
                }
            },
            rules: {
                companyDto: {
                    name: [
                        { required: true, message: '请输入机构名称', trigger: 'blur' },
                    ],
                    cpCode: [
                        { required: true, validator: rulesUtil.FormValidate.Form().checkUnifycode, trigger: 'blur' },
                    ],
                },
                user: {
                    userName: [
                        { required: true, message: '请输入登录账号', trigger: 'blur' },
                    ],
                    nickName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    phone: [
                        { validator: rulesUtil.FormValidate.Form().validatePhonNo, trigger: 'blur' },
                    ],
                    // avatar: [
                    //     { required: true, message: '请上传', trigger: 'change' },
                    // ],
                    password: [
                        { required: true, validator: rulesUtil.FormValidate.Form().validatePsdReg, trigger: 'blur' },
                    ],
                    checkpassword: [
                        {
                            required: true,
                            validator: checkPass2,
                            trigger: "blur",
                        },
                    ],
                }

            },
        }
    },
    created() {
    },
    methods: {
        // 用户注册校验唯一性--用户账号、手机号码、邮箱账号
        handleOnlyVal(type, val) {
            if (val) {
                let params = {
                    type: type,
                    value: val
                }
                getOnly(params).then(res => {
                    if (res.data.code === 500) {
                        this.$message({
                            message: res.data.message,
                            type: 'warning'
                        });
                        if (type === '用户账号') {
                            this.form.user.userName = ''
                        } else if (type === '手机号码') {
                            this.form.user.phone = ''
                        } 
                    }
                })
            }

        },
        // 判断密码强弱
        checkPassWord(value) {
            // 0： 表示第一个级别 1：表示第二个级别 2：表示第三个级别
            // 3： 表示第四个级别 4：表示第五个级别
            var modes = 0;
            if (value.length < 6) {
                //最初级别
                return modes;
            }
            if (/\d/.test(value)) {
                //如果用户输入的密码 包含了数字
                modes++;
            }
            if (/[a-zA-Z]/.test(value)) {
                //如果用户输入的密码 包含了小写的a到z
                modes++;
            }
            // if (/[A-Z]/.test(value)) {
            //     //如果用户输入的密码 包含了大写的A到Z
            //     modes++;
            // }
            if (/\W/.test(value)) {
                //如果是非数字 字母 下划线
                modes++;
            }
            switch (modes) {
                case 1:
                    return 1;
                    break;
                case 2:
                    return 2;
                    break;
                case 3:
                    return 3;
                    break;
            }
        },
        // 下一步-结果页面
        handleSubmit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading=true
                    let params = JSON.parse(JSON.stringify(this.form));
                    params.user.password = aesEncrypt(this.form.user.password);
                    delete params.user.checkpassword
                    console.log(params)
                    enterpriseRegNew(params).then(res => {
                        this.loading=false
                        if (res.data.code === 200) {
                            this.$message({
                                message: '注册成功！',
                                type: 'success'
                            });
                            this.$emit('hanldeResult')
                        } else {
                            this.$message.error(res.data.message);
                        }
                    })
                }
            });
        },
        // 上一步,返回类型页面
        prevBtn() {
            this.$emit('handleType')
        },
        // 上传文件成功-身份证正面
        handleAvatarSuccess(response, file) {
            this.imgUrl = URL.createObjectURL(file.raw);
            this.form.user.avatar = response.message;
            this.$refs.headimgUpload1.clearValidate(); // 关闭校验
        },
        // 上传文件失败-身份证正面
        handleError(err) {
            let errInfo = JSON.parse(err["message"]);
            this.$message.error(`文件上传失败，原因：${errInfo.message}`);
        },
        // 限制上传的类型
        beforeAvatarUpload(file) {
            const isLt10M = file.size / 1024 / 1024 < 10;
            if (!isLt10M) {
                this.$message.error("上传图片大小不能超过 10M!");
                return false;
            }
            var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
            const extension = testmsg === "jpg" || testmsg === "JPG";
            const extension2 = testmsg === "png" || testmsg === "PNG";
            if (!extension && !extension2) {
                this.$message({
                    message: "上传图片只能是 jpg, png格式!",
                    type: "error",
                });
                return false;
            }
        },
    },
    watch: {
        "form.user.password": {
            handler: function (val, oldVal) {
                if (val.length == "") {
                    this.passwordIndex = -1;
                } else {
                    this.passwordIndex = this.checkPassWord(val);
                }
            },
        },
    },
}
</script>
<style lang="less" scoped>
@import "./common.less";
</style>

